package com.ticket.sass.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.hall.HallListInput;
import com.ticket.sass.admin.mapper.HallMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.HallEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2022-09-26
 */
@Repository
public class HallServiceImpl extends ServiceImpl<HallMapper, HallEntity> implements BaseService<HallEntity> {

    public IPage<HallEntity> iPage(HallListInput input, Long cinemaId) {
        Page<HallEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        QueryWrapper<HallEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like(StrUtil.isNotEmpty(input.getName()), "name", input.getName());
        queryWrapper.eq("cinema_id", cinemaId);
        return this.page(page, queryWrapper);
    }
    
    public boolean isEmptyByCode(String code) {
        return count(new QueryWrapper<HallEntity>().eq("code", code)) > 0;
    }
    
    public List<String> codes(Integer cinemaId) {
        QueryWrapper<HallEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cinema_id", cinemaId);
        queryWrapper.select("code");
        return listObjs(queryWrapper, Object::toString);
    }
    
    public Map<Long, List<HallEntity>> queryHallsByPriceSchemeIds(List<Long> priceSchemeIds) {
        if (priceSchemeIds == null || priceSchemeIds.isEmpty()) {
            return new HashMap<>();
        }
        
        // 将价格方案ID列表转换为逗号分隔的字符串
        String priceSchemeIdsString = String.join(",", priceSchemeIds.stream().map(String::valueOf).collect(Collectors.toList()));
        
        // 调用新的查询方法，接受一个字符串参数
        List<HallEntity> halls = this.baseMapper.queryHallsByPriceSchemeIds(priceSchemeIdsString);
        
        // 构建结果映射
        Map<Long, List<HallEntity>> resultMap = new HashMap<>();
        for (HallEntity hall : halls) {
            Long priceSchemeId = hall.getPriceSchemeId(); // Assuming there is a method to get the price scheme id from HallEntity
            if (!resultMap.containsKey(priceSchemeId)) {
                resultMap.put(priceSchemeId, new ArrayList<>());
            }
            resultMap.get(priceSchemeId).add(hall);
        }
        
        return resultMap;
    }

    
    public Map<String, Long> getHallIdsByCodes(List<String> filmCodes, Long cinemaId, Long tenantId) {
        
        // 构建查询条件，根据 code 列表查询
        QueryWrapper<HallEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", filmCodes);
        queryWrapper.eq("cinema_id", cinemaId);
        queryWrapper.eq("tenant_id", tenantId);
        
        // 调用 MyBatis-Plus 的方法查询数据库
        List<HallEntity> entities = baseMapper.selectList(queryWrapper);
        
        // 构建 Map 以 code 为键，filmId 为值
        Map<String, Long> resultMap = new HashMap<>();
        for (HallEntity entity : entities) {
            resultMap.put(entity.getCode(), entity.getId());
        }
        
        return resultMap;
    }
    
    public List<String> codes(Long cinemaId){
        QueryWrapper<HallEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cinema_id", cinemaId);
        queryWrapper.select("code");
        return listObjs(queryWrapper, Object::toString);
    }
    
    public List<HallEntity> queryHallsByCouponId(Long couponId) {
        return this.baseMapper.queryHallsByCouponId(couponId);
    }


}
